package com.yilin.tms.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yilin.commons.util.StringUtil;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.annotation.Explain;
import com.yilin.tms.core.commons.annotation.NotNull;
import com.yilin.tms.core.commons.annotation.UserTarget;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.commons.utils.QueryUtil;
import com.yilin.tms.core.entity.order.party.OrderSpecialApply;
import com.yilin.tms.core.entity.order.transit.TransitOrder;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.order.service.order.ITransitService;
import com.yilin.tms.order.service.party.ISpecialApplyService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 仲裁控制器
 *
 * @author zhangjinyuan
 * @date 2022/10/31
 */
@RestController
@RequestMapping("/order/special/")
public class SpecialApplyController extends BaseController {

    @Resource
    ISpecialApplyService specialApplyService;
    @Resource
    ITransitService transitService;

    /**
     * 获取仲裁分页
     */
    @PostMapping(value = "getSpecialApplyPage")
    @NotNull({"page", "limit"})
    @UserTarget({UserType.adminUser,UserType.platformUser})
    public ReturnData<PageData<OrderSpecialApply>> getSpecialApplyPage(OrderSpecialApply query, @Explain("页码") Integer page, @Explain("条数") Integer limit) {
        QueryWrapper<OrderSpecialApply> queryWrapper = new QueryWrapper<>();
        QueryUtil.andQueryWrapper(queryWrapper, query);
        PageData<OrderSpecialApply> pageData = specialApplyService.page(new PageData<>(page, limit), queryWrapper);
        return jsonView(true, "获取仲裁列表成功", pageData);
    }

    /**
     * 获取仲裁详情
     */
    @PostMapping(value = "getSpecialApplyById")
    @NotNull("specialApplyId")
    public ReturnData<OrderSpecialApply> getSpecialApplyById(@Explain("仲裁ID") String specialApplyId) {
        OrderSpecialApply orderSpecialApply = specialApplyService.getById(specialApplyId);
        return jsonView(true, "获取成功", orderSpecialApply);
    }

    /**
     * 申请仲裁
     */
    @PostMapping(value = "settlementArbitration")
    @NotNull({"arbitrationType", "transitOrderId", "remark"})
    @UserTarget({UserType.shipperUser, UserType.truckerUser})
    public ReturnData<Object> settlementArbitration(OrderSpecialApply orderSpecialApply) {
        if (orderSpecialApply.getRemark().length() > 100) return jsonView(false, "申述原因不能超过100字符");
        TransitOrder transitOrder = transitService.getById(orderSpecialApply.getTransitOrderId());
        ParamUtil.isNull(transitOrder, "运单不存在");
        if (getLoginUser().getUserType() == UserType.shipperUser) ParamUtil.notEquals("不是你的运单不能申请仲裁", transitOrder.getShipperAccountId(), getLoginUser().getId());
        if (getLoginUser().getUserType() == UserType.truckerUser) ParamUtil.notEquals("不是你的运单不能申请仲裁", transitOrder.getTransitTruckUserId(), getLoginUser().getId());
        ParamUtil.equals("运单已送达不能申请仲裁", transitOrder.getTransitStatus(), TransitOrder.TransitStatus.waitingSettlement);
        ParamUtil.equals("运单已完成不能申请仲裁", transitOrder.getTransitStatus(), TransitOrder.TransitStatus.complete);
        ParamUtil.equals("运单已经作废不能申请仲裁", transitOrder.getTransitStatus(), TransitOrder.TransitStatus.scraped);
        ParamUtil.equals("运单已申请仲裁请等待仲裁结果", transitOrder.getLockingStatus(), TransitOrder.LockingStatus.locking);
        ParamUtil.equals("运单锁定中", transitOrder.getOperateStatus(), TransitOrder.LockingStatus.locking);
        specialApplyService.applyArbitration(getLoginUser(), transitOrder, orderSpecialApply);
        return jsonView(SUCCESS_CODE, "仲裁提交成功", null);
    }

    /**
     * 处理仲裁
     */
    @PostMapping(value = "editSpecialApply")
    @UserTarget({UserType.platformUser})
    public ReturnData<Object> editSpecialApply(OrderSpecialApply orderSpecialApply) {
        if (orderSpecialApply.getProcessingResult() == OrderSpecialApply.ProcessingResult.reimburse && orderSpecialApply.getIndemnity() == null) {
            return jsonView(false, "赔偿金额不能为空");
        }
        if (orderSpecialApply.getProcessingResult() != OrderSpecialApply.ProcessingResult.reimburse && orderSpecialApply.getIndemnity() != null) {
            return jsonView(false, "只有【赔偿司机】才能添加赔偿金额");
        }
        if (orderSpecialApply.getIndemnity() == null) orderSpecialApply.setIndemnity(0D);
        if (StringUtil.isEmpty(orderSpecialApply.getDealtRemark())) orderSpecialApply.setDealtRemark("无");
        if (orderSpecialApply.getDealtRemark().length() > 100) return jsonView(false, "处理说明长度不能超过100字符");
        if (orderSpecialApply.getCredit() == null) orderSpecialApply.setCredit(0D);
        OrderSpecialApply localOrderSpecialApply = specialApplyService.getById(orderSpecialApply.getId());
        ParamUtil.isNull(localOrderSpecialApply, "未找到仲裁信息");
        ParamUtil.equals("该仲裁已处理", localOrderSpecialApply.getSpecialStatus(), OrderSpecialApply.SpecialStatus.complete);
        TransitOrder transitOrder = transitService.getById(localOrderSpecialApply.getTransitOrderId());
        ParamUtil.isNull(transitOrder, "未查询到该运单");
        ParamUtil.notEquals("运单已解锁", transitOrder.getLockingStatus(), TransitOrder.LockingStatus.locking);
        ParamUtil.equals("运单已作废", transitOrder.getTransitStatus(), TransitOrder.TransitStatus.scraped);
        ParamUtil.equals("该运单已经完结不能操作", transitOrder.getTransitStatus(), TransitOrder.TransitStatus.complete);
        ParamUtil.equals("该运单已经送达不能操作", transitOrder.getTransitStatus(), TransitOrder.TransitStatus.waitingSettlement);
        specialApplyService.editSpecialApply(getLoginUser(), orderSpecialApply, localOrderSpecialApply, transitOrder);
        return jsonView(true, "处理成功");
    }
}
